<?php

namespace App\Plugins\Article\Controller;

use App\Plugins\Article\Model\Article as Article;

use Illuminate\Routing\Controller as Controller;
use Illuminate\Contracts\Validation\Validator;

//use Illuminate\Http\Request as Request;

class ArticleController extends Controller
{


    /*
        列出文章
    */

    public function articlelist(){

        $articles = Article::all();
        return view('article::list', ['articles'=>$articles]);

    }

    /*
        创建新的文章
    */
    public function articleaddpost(\App\Plugins\Article\Requests\Article $request){

        
        $article = new Article;
        $article->title = $request->input('title');
        $article->content = $request->input('content');

        $result = $article->save();
        if ($result){
            return '恭喜您新增成功! <a href="/authtest/public/article/list">返回列表</a>';
        }
    }

    /*
        编辑文章
    */

    public function articleeditpost(Request $request, $id){

        $this->validate($request, [
            'title' => 'required',
            'content' => 'required',
        ]);

        $article = Article::find($id);
        $article->title = $request->input('title');
        $article->content = $request->input('content');

        $result = $article->save();
        if ($result){
            return '文章编辑成功! <a href="/authtest/public/article/list"'. $id.'>点此返回</a>';
        }


    }

    public function articleeditget($id){
        $article = Article::find($id);
        return view('article::edit', ['article'=>$article]);
    }

    public function articledelget($id){
        $article = Article::find($id);
        $result = $article->delete();

        if ($result){
            return '文章删除成功! <a href="/authtest/public/article/list"'. $id.'>点此返回</a>';
        }
    }

    /*
     *
     *  查看博客文章
     *
     * */

    public function articleshowget($id){
        $article = Article::find($id);
        return view('article::show', ['article'=>$article]);
    }

    protected function formatErrors(Validator $validator){
        return $validator->errors()->all();
    }

    public function messages(){
        return [
            'title.required' => 'A title is required',
            'content.required'  => 'A content is required',
        ];
    }



}